#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>

using namespace std;


int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
    int n = tops.size();
    int top[7] = { 0 };
    int bottom[7] = { 0 };
    for (int i = 0; i < n; ++i) {
        if (tops[i] == bottoms[i]) {
            if(top[tops[i]] >= bottom[bottoms[i]])
				++top[tops[i]];
			else ++bottom[bottoms[i]];
        }
            
        else {
            top[tops[i]]++;
            bottom[bottoms[i]]++;
        }
    }

    int ans = n;
    for (int i = 1; i <= 6; ++i) {
        if (top[i] + bottom[i] == n)
            ans = min({ans, top[i], bottom[i]});
    }
    return ans == n ? -1 : ans;
}


int main() {
	vector<int> tops = { 1,2,1,1,1,2,2,2 };
	vector<int> bottoms = { 2,1,2,2,2,2,2,2 };
	cout << minDominoRotations(tops, bottoms) << endl;
	return 0;
}